﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Models.Interact;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels.Dialogs
{
    public class NewScanSignalViewDialog : Domains.ViewModelBase
    {
        public IEnumerable<DeviceTypes> DeviceList
        {
            get
            {
                foreach (DeviceTypes c in (DeviceTypes[])Enum.GetValues(typeof(DeviceTypes)))
                {
                    yield return c;
                }
            }
        }
        public IEnumerable<int> DeviceIndexList
        {
            get
            {
                for (int i = 1; i < 20; i++)
                {
                    yield return i;
                }
            }
        }
        private bool _isUpdate = false;
        private System.Windows.Visibility _sUpdate;

        public System.Windows.Visibility IsUpdate
        {
            get { return _sUpdate; }
            set { SetProperty(ref _sUpdate, value); }
        }

        private SystemConfigStatic _configStatic;
        public ScanSignalModel IAM { get; set; }
        public NewScanSignalViewDialog(SystemConfigStatic configStatic, ScanSignalModel model, bool isUpdate = false)
        {
            _configStatic = configStatic;
            _isUpdate = isUpdate;
            if (_isUpdate)
            {
                IAM = model;
                IsUpdate = System.Windows.Visibility.Visible;
            }
            else
            {
                IsUpdate = System.Windows.Visibility.Collapsed;
                IAM = new ScanSignalModel();
            }
        }

        public Commands.DelegateCommand Confirm => new Commands.DelegateCommand(() =>
        {
            if (string.IsNullOrEmpty(IAM.Name))
            {
                if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
                return;
            }
            if (!_isUpdate)
            {
                Guid guid = Guid.NewGuid();
                _configStatic.SignalList.Add(new ScanSignalModel()
                {
                    Address_Start = IAM.Address_Start,
                    Length_Resection = IAM.Length_Resection,
                    Length_Signal = IAM.Length_Signal,
                    DeviceType = IAM.DeviceType,
                    Name = IAM.Name,
                    Heartbeat = IAM.Heartbeat,
                    ThisGuid = guid,
                });
            }
            _configStatic.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });

        public Commands.DelegateCommand Delete => new Commands.DelegateCommand(() =>
        {
            _configStatic.SignalList.Remove(IAM);
            _configStatic.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });
    }
}
